package io.zt.dangyuan;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;

import org.xutils.ex.HttpException;

import io.zt.dangyuan.object.AddressBook;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class AddressBookActivity extends BaseActivity {

    private ListView listView;
    private AddressBookAdapter addressListAdapter;
    private List<AddressBook.ListEntity> datas;

    private String  groupid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addCenterView(R.layout.activity_address_book);

        setTitle("通讯录");
        toolbar.setNavigationIcon(R.drawable.icon_pre);
        groupid  = getIntent().getStringExtra("groupid");

        datas = new ArrayList<AddressBook.ListEntity>();
        listView   = (ListView) findViewById(R.id.address_book_listView);
        addressListAdapter =  new AddressBookAdapter(this,datas);

        listView.setAdapter(addressListAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

//                Intent intent = new Intent(AddressBookActivity.this, AddressBookActivity.class);
//                AddressList.ListEntity object = datas.get(position - 1);
//                intent.putExtra("groupid", object.getGroupid());
//                startActivity(intent);

            }
        });

    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
//        getMenuInflater().inflate(R.menu.menu_address_book, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
//        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void webRequst() {

        HashMap<String,Object> map = new HashMap<String,Object>();
        map.put("groupid",groupid);
        WebUtils.sendPost(AddressBookActivity.this, "groupInfoList", map, new WebUtils.OnWebListenr() {
            @Override
            public void onSuccess(boolean isSuccess, String msg, String data) {

                AddressBook resultData  = JSON.parseObject(data, AddressBook.class);
                datas.clear();
                datas.addAll(resultData.getList());
                addressListAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailed(HttpException error, String msg) {

            }
        });


    }

}
